﻿CREATE TABLE [dbo].[VersionInfo](
	[FarmId] [uniqueidentifier] NOT NULL,
	[Version] [tinyint] NOT NULL,
	[Created] [datetime] NOT NULL,
	[CreatedBy] [nvarchar](50) NOT NULL,
	[LicenseKey] [nvarchar](200) NULL,
 CONSTRAINT [PK_VersionInfo] PRIMARY KEY CLUSTERED 
(
	[FarmId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
-- =============================================
CREATE PROCEDURE [dbo].[GetVersionInfo]
	@FarmId UniqueIdentifier
AS
BEGIN
	select 
		Version, Created, CreatedBy, LicenseKey 
	from
		VersionInfo
	where
		FarmId=@FarmId
END
GO
-- =============================================
CREATE PROCEDURE [dbo].[SetVersionInfo]
	@Version tinyint,
	@FarmId UniqueIdentifier,
	@CreatedBy nvarchar(50),
	@LicenseKey nvarchar(200)
AS
BEGIN
if exists(select Version from VersionInfo where FarmId=@FarmId)
begin
	update VersionInfo set
		Version = @Version,
		FarmId=@FarmId,
		CreatedBy=@CreatedBy,
		Created= getdate(),
		LicenseKey=@LicenseKey
	where
		FarmId = @FarmId
end
else
begin
	insert into VersionInfo
		(Version, Created, CreatedBy, FarmId, LicenseKey)
	values
		(@Version, getdate(), @CreatedBy, @FarmId, @LicenseKey)
end
END
GO